<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="rodesList">
        <el-table-column type="expand" label="展开">
          <template slot-scope="scope">
            <el-row :class="['bdbottom',i1===0?'bdtop':'']" v-for="(item1,i1) in scope.row.children" :key="item1.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightsJurisdiction(scope.row,item1.id)">
                  {{item1.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二权限 -->
              <el-col :span="19">
                <el-row :class="[i2===0?'':'bdtop' ]" v-for="(item2,i2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightsJurisdiction(scope.row,item2.id)">
                      {{item2.authName}}</el-tag>
                    <i class=" el-icon-caret-right"></i>
                  </el-col>
                  <!-- 渲染三 -->
                  <el-col :span="18">
                    <el-tag type="warning" closable @close="removeRightsJurisdiction(scope.row,item3.id)"
                      v-for="(item3) in item2.children" :key="item3.id">
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <!-- 编辑按钮 -->
            <el-tooltip effect="light" content="编辑" placement="left">
              <el-button type="primary" size="mini" icon="el-icon-edit">编辑 </el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-tooltip content="删除" placement="top" effect="light">
              <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            </el-tooltip>
            <el-tooltip content="分配权限" placement="right" effect="light">
              <!-- 角色权限 -->
              <el-button type="warning" size="mini" icon="el-icon-setting" @click="showDialog(row)">角色权限</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 角色权限弹窗 -->
    <el-dialog title="权限控制选择列表" width="30%" :visible.sync='showRightDialo'>
      <!-- 树形控件 -->
      <el-tree ref="myTree" :data="rightsListTree" :default-checked-keys='decfokeys' show-checkbox default-expand-all
        node-key="id" :props="treeProps">
      </el-tree>
      <template>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showRightDialo = false">取 消</el-button>
          <el-button type="primary" @click="saveJurisdiction">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 控制权限窗口的现实与隐藏
      showRightDialo: false,
      // 所有角色列表数据
      rodesList: [],
      // 用户权限管理tree
      rightsListTree: [],
      // 默认选中的 id  数组
      decfokeys: [],
      rolesId: '' // 角色ID
    }
  },
  methods: {
    // 收集所有选中的  全选和半选
    async saveJurisdiction () {
      // 收集全选数据
      const saveTree1 = this.$refs.myTree.getCheckedKeys()
      // 收集半选数据
      const saveTree2 = this.$refs.myTree.getHalfCheckedKeys()
      // 合并数据
      const rids = [...saveTree1, ...saveTree2].join(',')
      console.log(rids)
      await this.$http.post(`roles/${this.rolesId}/rights`, { rids })
      this.showRightDialo = false // 关闭弹窗
      this.getRolesList()
    },
    // 点击事件窗口弹出
    async showDialog (row) {
      this.rolesId = row.id
      const { data: res } = await this.$http.get('rights/tree')
      this.rightsListTree = res.data
      console.log(this.rightsListTree)
      const arrList = []
      row.children.forEach(item1 => {
        if (item1.children && item1.children.length > 0) {
          item1.children.forEach(item2 => {
            if (item2.children && item2.children.length > 0) {
              item2.children.forEach(item3 => {
                arrList.push(item3.id)
              })
            }
          })
        }
      })
      this.decfokeys = arrList
      this.showRightDialo = true
    },
    // 删除权限  promise函数
    removeRightsJurisdiction (row, rightId) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${row.id}/rights/${rightId}`)
        row.children = res.data
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '删除已取消'
        })
      })
    },
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      this.rodesList = res.data
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () {
    this.getRolesList()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.el-tag {
  margin: 7px;
}

.bdtop {
  border-top: 1px solid #eee;
}

.bdbottom {
  border-bottom: 1px solid #eee;
}
</style>
